import $ from 'jquery'
// 这个js是负责整理dom节点的css样式
var unitArr = ['width', 'height', 'top', 'left', 'right', 'bottom'] // 需要单位的数组

// styleArr 写在dome上的样式
// domJson 后台 返回的数据
// unit 单位，一般是px或者%
function computerCss(styleArr, domJson, unit) {
  var styleStr = ''
  styleArr.map((item) => {
    for (var j in item) {
      if (
        unitArr.some(function (it) {
          return it == j
        })
      ) {
        styleStr += j + ':' + domJson[item[j]] + unit + ';'
      } else {
        styleStr += j + ':' + domJson[item[j]] + ';'
      }
    }
  })
  //
  return styleStr
}

computerCss(
  ['width', 'height', { top: 'y' }, { left: 'x' }],
  {
    wordWrapEnabled: 'false',
    image: 'null',
    visible: true,
    textToIconAlign: 'Right',
    blink: 'false',
    foreground: 'null',
    textIconGap: '4.0',
    type: 'bajaui:Label',
    background: 'null',
    name: 'Label',
    width: 100,
    x: 340,
    childWidgets: [],
    y: 170,
    text: '0990909',
    font: 'null',
    height: 20,
  },
  'px'
)

// 计算出来宽高top和left
function computerWidthHeightTopLeft(data) {
  if (data.viewSize) {
    var arrSplit = data.viewSize.split(',')
    var width = arrSplit[0]
    var height = arrSplit[1]
    if (arrSplit.length == 4) {
      var left = arrSplit[2]
      var top = arrSplit[3]
    }
    data.width = width
    data.height = height
    data.x = data.left
    data.y = data.top
  } else if (data.layout) {
    var arrSplit = data.layout.split(',')
    var left = arrSplit[0]
    var top = arrSplit[1]
    if (arrSplit.length == 4) {
      var width = arrSplit[2]
      var height = arrSplit[3]
    }
    data.width = width
    data.height = height
    data.left = left
    data.top = top
  } else {
  }
}

export { computerCss, computerWidthHeightTopLeft }
